﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VietPublisher.BSL.BK;
using VietPublisher.Repository.Repositories.Concrete;

namespace VietPublisher.Repositories.Interface
{
   public class CategoryRepository: BaseRepository, ICategoryRepository
    {
        public CategoryRepository() { 
        }

        public List<T_BK_Category> GetCategorys()
        {
            var categories = ContextBookStore.T_BK_Category.OrderBy(c=>c.IsAdmin).ToList();
            return categories;
        }

        public T_BK_Category GetParentCategory(int categoryChildID)
        {
            var category = ContextBookStore.T_BK_Category.Where(p => p.SubCategoryID == categoryChildID).FirstOrDefault();
            return category;
        }

        public List<T_BK_Category> GetChildCategory(int categoryParentID)
        {
            var categories = ContextBookStore.T_BK_Category.Where(p => p.CategoryID == categoryParentID).ToList();            
            return categories;
        }

        public T_BK_Category GetCategoryByBookID(int bookID)
        {
            var category = (
                           from cate in ContextBookStore.T_BK_Category
                           join bk in ContextBookStore.T_BK_Book 
                           on cate.CategoryID equals bk.CategoryID 
                           select cate
                           )
                           .FirstOrDefault();
            return category;
        }

        public String GetCategoryName(int categoryID)
        {
            String categoryName = String.Empty;
            var category = ContextBookStore.T_BK_Category.Where(p => p.CategoryID == categoryID).FirstOrDefault();
            if (category != null) {
                categoryName = category.CategoryName;
            }
            return categoryName;
        }

        public string GetCategoryCode(int categoryID)
        {
            String categoryCode = String.Empty;
            var category = ContextBookStore.T_BK_Category.Where(p => p.CategoryID == categoryID).FirstOrDefault();
            if (category != null)
            {
                categoryCode = category.CategoryCode;
            }
            return categoryCode;
        }

        public int GetCategoryLevel(int categoryID)
        {
            int level = 0;
            var category = ContextBookStore.T_BK_Category.Where(p => p.CategoryID == categoryID).FirstOrDefault();
            if (category != null)
            {
                level = category.CategoryLevel.Value;
            }
            return level;
        }

        public List<T_BK_Category> GetCategoryAdmin()
        {
            var categories = ContextBookStore.T_BK_Category.Where(p => p.IsAdmin == true).OrderBy(m=>m.Position).ToList();
            return categories;
        }

        public List<T_BK_Category> GetCategoryCustomer()
        {
            var categories = ContextBookStore.T_BK_Category.Where(p => p.IsAdmin == false).OrderBy(m=>m.Position).ToList();
            return categories;
        }
    }
}
